package inits

import (
	"fmt"
	"github.com/google/uuid"
	"github.com/hashicorp/consul/api"
	"github.com/spf13/viper"
	"log"
	"monthly/biz/dal/global"
)

func init() {
	InitViper()
}
func InitViper() {
	config := api.DefaultConfig()
	config.Address = "14.103.243.15:8500"
	client, _ := api.NewClient(config)
	reqParams := new(api.AgentServiceRegistration)
	reqParams.Address = "127.0.0.1"
	reqParams.Port = 8080
	serviceID := uuid.NewString()
	reqParams.ID = serviceID
	reqParams.Tags = []string{"client", "consul"}
	reqParams.Name = "name"
	err := client.Agent().ServiceRegister(reqParams)
	if err != nil {
		panic("consul连接失败")
	}
	log.Println("consul连接成功")

	viper.SetConfigFile("biz/dal/dev.yaml")
	err = viper.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("viper读取失败: %s \n", err))
	}
	log.Println("viper读取成功")
	err = viper.Unmarshal(&global.AppConf)
	if err != nil {
		panic(fmt.Errorf("viper解析失败: %s \n", err))
	}
	log.Println("viper解析成功", global.AppConf)
}
